package com.sj.parksdk.adapter.pay;

import android.view.View;
import android.view.animation.OvershootInterpolator;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.vlayout.layout.GridLayoutHelper;
import com.sj.parksdk.R;
import com.sj.parksdk.adapter.base.delegate.SimpleDelegateAdapter;
import com.sj.parksdk.net.result.databean.Data_Purchase;
import com.xuexiang.xaop.annotation.SingleClick;
import com.xuexiang.xui.adapter.recyclerview.BaseRecyclerAdapter;
import com.xuexiang.xui.adapter.recyclerview.RecyclerViewHolder;
import com.xuexiang.xui.utils.DensityUtils;
import com.xuexiang.xui.utils.ResUtils;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.layout.ExpandableLayout;

import java.util.Collection;
import java.util.List;

public class AdapterExpandableListPurchase extends BaseRecyclerAdapter<Data_Purchase> {

    private RecyclerView mRecyclerView;
    private int [] selectedType ;

    public AdapterExpandableListPurchase(RecyclerView recyclerView, Collection<Data_Purchase> data) {
        super(data);
        mRecyclerView = recyclerView;
        if(data == null || data.size() == 0)return;
        selectedType = new int[data.size()];
    }

    public AdapterExpandableListPurchase(RecyclerView recyclerView) {
        mRecyclerView = recyclerView;
    }

    public void refreshData(Collection<Data_Purchase> collection) {
        super.refresh(collection);
        if(mData == null || mData.size() == 0)return;
        selectedType = new int[mData.size()];
    }

    public int getChildSelectedPosition(){
        if(mSelectPosition == -1)return mSelectPosition;
        return selectedType == null||selectedType.length ==0 ?-1:selectedType[mSelectPosition];
    }

    public Data_Purchase.TypeBean getChildSelectedItem(){
        if(mSelectPosition == -1)return null;
        if(getChildSelectedPosition() == -1)return null;
        List<Data_Purchase.TypeBean> childs = mData.get(mSelectPosition).getItems();
        if(childs == null || childs.size() == 0)return null;
        return childs.get(getChildSelectedPosition());
    }

    @Override
    protected int getItemLayoutId(int viewType) {
        return R.layout.parksdk_adapter_purchase_detail;
    }

    @Override
    protected void bindData(@NonNull RecyclerViewHolder holder, int parentPosition, Data_Purchase item) {
        ExpandableLayout expandableLayout = holder.findViewById(R.id.expl);
        AppCompatImageView ivIndicator = holder.findViewById(R.id.iv_indicator);
        expandableLayout.setInterpolator(new OvershootInterpolator(1.0f));
        expandableLayout.setOnExpansionChangedListener((expansion, state) -> {
            if (mRecyclerView != null && state == ExpandableLayout.State.EXPANDING) {
                mRecyclerView.smoothScrollToPosition(parentPosition);
            }
            if (ivIndicator != null) {
                ivIndicator.setRotation(expansion * 180);
            }
        });

        List<Data_Purchase.TypeBean> listItem = item.getItems();
        if (listItem != null){
            RecyclerView recyclerGrid = holder.findViewById(R.id.recycler_purchase_type);
            WidgetUtils.initGridRecyclerView(recyclerGrid,2, DensityUtils.dp2px(13),ResUtils.getColor(R.color.color_white));
            SimpleDelegateAdapter<Data_Purchase.TypeBean> adapterGridType = new SimpleDelegateAdapter<Data_Purchase.TypeBean>(R.layout.parksdk_adapter_gride_purchase_price,new GridLayoutHelper(2),listItem) {
                @Override
                protected void bindData(@NonNull RecyclerViewHolder holder, int childPosition, Data_Purchase.TypeBean item) {
                    holder.text(R.id.tv_price,"¥"+item.getMoney().toString());
                    holder.text(R.id.tv_month,item.getMonth()+"个月");
                    boolean selected = getSelectPosition() == childPosition;
                    holder.select(R.id.tv_price,selected);
                    holder.select(R.id.tv_month,selected);
                    holder.select(R.id.rrl_item,selected);
                    ImageView ivSelect = holder.findViewById(R.id.iv_select);
                    ivSelect.setColorFilter(selected?
                            ResUtils.getResources().getColor(R.color.color_white):
                            ResUtils.getResources().getColor(R.color.color_ic_gray));
                    holder.click(R.id.rrl_item,v-> onChildClickItem(this,childPosition));
                }
            };
            recyclerGrid.setAdapter(adapterGridType);

            boolean isSelected = parentPosition == mSelectPosition;
            expandableLayout.setExpanded(isSelected, false);

            if(listItem!=null&& listItem.size()>0){
                adapterGridType.setSelectPosition(0);
            }
        }

        holder.text(R.id.tv_title, item.getName());

        holder.click(R.id.ll_title, new View.OnClickListener() {
            @SingleClick
            @Override
            public void onClick(View v) {
                onClickItem(v, expandableLayout, parentPosition);
            }
        });
    }

    private void onChildClickItem(SimpleDelegateAdapter<Data_Purchase.TypeBean> adapter,int childPosition){
        if(adapter == null)return;
        adapter.setSelectPosition(childPosition);
        if(mSelectPosition == -1 || selectedType == null || selectedType.length == 0)return;
        selectedType[mSelectPosition] = childPosition;
    }


    private void onClickItem(View view, final ExpandableLayout expandableLayout, final int position) {
        RecyclerViewHolder holder = (RecyclerViewHolder) mRecyclerView.findViewHolderForAdapterPosition(mSelectPosition);
        if (holder != null) {
            holder.select(R.id.ll_title, false);
            ((ExpandableLayout) holder.findViewById(R.id.expl)).collapse();
        }

        if (position == mSelectPosition) {
            mSelectPosition = -1;
        } else {
            expandableLayout.expand();
            mSelectPosition = position;
        }
    }
}
